package edu.thu.studio.ormdesigner.editor.command;

import edu.thu.studio.ormdesigner.common.AbstractCommand;
import edu.thu.studio.ormdesigner.model.diagram.Diagram;
import edu.thu.studio.ormdesigner.model.diagram.node.TableNode;
import edu.thu.studio.ormdesigner.model.orm.table.Table;
import edu.thu.studio.ormdesigner.util.Utils;

public class DeleteTableNodeCommand extends AbstractCommand {
    
    private Diagram diagram;
    
    private TableNode node;
    
    private Table table;
    
    public DeleteTableNodeCommand(Diagram diagram, TableNode node) {
        this.diagram = diagram;
        this.node = node;
    }
    
    @Override
    protected void doExecute() {
        this.diagram.getNodes().remove(node);
        if (node.isOriginal()) {
            table = Utils.getTable(node.getRefId());
            Utils.getTableSet().remove(table);
        }
        this.diagram.refreshChildren();
    }
    
    @Override
    protected void doUndo() {
        this.diagram.getNodes().addObject(node);
        if (node.isOriginal()) {
            Utils.getTableSet().addObject(table);
        }
        this.diagram.refreshChildren();
    }
    
}
